﻿using System;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Data;
using YiSha.Data.Repository;
using YiSha.Entity.QixingManage;
using YiSha.Model.Param.QixingManage;

namespace YiSha.Service.QixingManage
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2023-03-19 19:30
    /// 描 述：抢单管理服务类
    /// </summary>
    public class PrebookOrderService :  RepositoryFactory
    {
        CustomerAddrService customerAddrService = new CustomerAddrService();
        #region 获取数据
        public async Task<List<PrebookOrderEntity>> GetList(PrebookOrderListParam param)
        {
            var expression = ListFilter(param);
            var list = await this.BaseRepository().FindList(expression);
            return list.ToList();
        }

        public async Task<List<PrebookOrderEntity>> GetPageList(PrebookOrderListParam param, Pagination pagination)
        {
            StringBuilder strSql = new StringBuilder();
            var parameter = new List<DbParameter>();
            strSql.Append(@"SELECT a.*,b.nickname,b.telephone AS usertelephone,c.brand_name FROM car_prebook_order  a JOIN
            customer_user b ON a.userid=b.Id left join brand_info c on a.car_bindid=c.Id where 1=1");

            if (!param.nickname.IsEmpty())
            {
                strSql.Append(" AND b.nickname like @nickname ");
                parameter.Add(DbParameterExtension.CreateDbParameter("@nickname", '%' + param.nickname + '%'));
            }
            if (!param.carordersn.IsEmpty())
            {
                strSql.Append(" AND b.car_order_sn like @carordersn ");
                parameter.Add(DbParameterExtension.CreateDbParameter("@carordersn", '%' + param.carordersn + '%'));
            }
            if (!param.status.IsEmpty())
            {
                if (param.status!=-1) 
                {
                    strSql.Append(" AND a.status=@status ");
                    parameter.Add(DbParameterExtension.CreateDbParameter("@status", param.status));
                }
            }
            if (!param.tenant_id.IsNullOrZero())
            {
                strSql.Append(" AND a.PreBookShopId in(select Id from shop_info where tenant_id=@tenant_id) ");
                parameter.Add(DbParameterExtension.CreateDbParameter("@tenant_id", param.tenant_id));
            }

            IEnumerable<PrebookOrderEntity> list = await this.BaseRepository().FindList<PrebookOrderEntity>(strSql.ToString(), parameter.ToArray(), pagination);

            foreach (var item in list)
            {
                if (item.telephone.IsEmpty()) 
                {
                    if (item.userid==null)
                    {
                        continue;
                    }
                    var useraddr=customerAddrService.GetUserEntity(item.userid.Value).Result;
                    if (useraddr!=null) 
                    {
                        item.telephone = useraddr.telephone;
                    }
                }
            }
            
            return list.ToList();
        }

        public async Task<PrebookOrderEntity> GetEntity(long id)
        {
            var data=await this.BaseRepository().FindEntity<PrebookOrderEntity>(id);

            //根据id获取车辆领取的详细信息
            var carlist=this.BaseRepository().IQueryable<CarPreBookDetail>(t => t.CarPreBookId == id).ToList();

            if (carlist!=null)
            {
                data.CarList.AddRange(carlist);
            }
            return data;
        }
        #endregion

        #region 提交数据
        public async Task SaveForm(PrebookOrderEntity entity)
        {
            if (entity.Id.IsNullOrZero())
            {
                entity.Create();
                await this.BaseRepository().Insert(entity);
            }
            else
            {
                
                await this.BaseRepository().Update(entity);
            }
        }

        public async Task DeleteForm(string ids)
        {
            long[] idArr = TextHelper.SplitToArray<long>(ids, ',');
            await this.BaseRepository().Delete<PrebookOrderEntity>(idArr);
        }
        #endregion

        #region 私有方法
        private Expression<Func<PrebookOrderEntity, bool>> ListFilter(PrebookOrderListParam param)
        {
            var expression = LinqExtensions.True<PrebookOrderEntity>();
            if (param != null)
            {
            }
            return expression;
        }
        #endregion
    }
}
